<template>
  <el-cascader
    :options="options"
    :props="props"
    :value="value"
    class="inputW"
    separator="-"
    size="mini"
    @change="onChangeValue"
  />
</template>
<script>

import { getDepartmentList } from '@/api/department'
import { transListToTreeData } from '@/utils'

export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      options: [],
      props: {
        label: 'name',
        value: 'id',
        expandTrigger: 'hover'
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 获取部门信息
    async getDepartment() {
      const res = await getDepartmentList()
      this.options = transListToTreeData(res, 0)
    },
    // 级联选择框变化时事件
    onChangeValue(valueList) {
      if (valueList.length > 0) {
        this.$emit('input', valueList[valueList.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>
